{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright 2021-2023 @ Shenzhen Bay Laboratory & Peking University & Huawei Technologies Co., Ltd\n",
    "\n",
    "This code is a part of MindSPONGE:\n",
    "MindSpore Simulation Package tOwards Next Generation molecular modelling.\n",
    "\n",
    "MindSPONGE is open-source software based on the AI-framework:\n",
    "MindSpore (https://www.mindspore.cn/)\n",
    "\n",
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "\n",
    "You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n",
    "\n",
    "MindSPONGE basic tutorial 00: Basic function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mindspore as ms\n",
    "from mindspore import Tensor\n",
    "from mindsponge import function as func"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Float32, value= 1.73205)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = Tensor([0, 0, 0], ms.float32)\n",
    "b = Tensor([1, 1, 1], ms.float32)\n",
    "\n",
    "func.calc_distance(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Float32, value= 45)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = Tensor([0, 0, 0], ms.float32)\n",
    "b = Tensor([0, 0, 1], ms.float32)\n",
    "c = Tensor([0, 1, 0], ms.float32)\n",
    "\n",
    "func.calc_angle(a, b, c) / func.PI * 180"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Float32, value= 90)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = Tensor([-1, 1, 0], ms.float32)\n",
    "b = Tensor([0, 0, 0], ms.float32)\n",
    "c = Tensor([0, 0, -1], ms.float32)\n",
    "d = Tensor([1, 1, -1], ms.float32)\n",
    "\n",
    "func.calc_torsion(a, b, c, d) / func.PI * 180"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "90.0\n",
      "90.0\n",
      "-90.0\n",
      "48.189682\n"
     ]
    }
   ],
   "source": [
    "print(func.calc_torsion(a, b, c, d) / func.PI * 180)\n",
    "print(func.calc_torsion(d, c, b, a) / func.PI * 180)\n",
    "print(func.calc_torsion(a, c, b, d) / func.PI * 180)\n",
    "print(func.calc_torsion(b, a, d, c) / func.PI * 180)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[3], dtype=Float32, value= [-2.00000000e+00, -2.00000000e+00, -2.00000000e+00])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = Tensor([1, 1, 1], ms.float32)\n",
    "b = Tensor([9, 9, 9], ms.float32)\n",
    "box = Tensor([10, 10, 10], ms.float32)\n",
    "\n",
    "func.calc_vector_pbc(a, b, box)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Float32, value= 3.4641)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "func.calc_distance(a, b, box)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mindsponge.function import Units\n",
    "units0 = Units('nm', 'kj/mol')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.200000 A = 0.120000 nm\n",
      "3.400000 nm = 34.000000 A\n"
     ]
    }
   ],
   "source": [
    "r_a = 1.2\n",
    "r_nm = r_a * units0.convert_length_from('A')\n",
    "print('{:>f} A = {:>f} nm'.format(r_a, r_nm))\n",
    "r_nm = 3.4\n",
    "r_a = r_nm * units0.convert_length_to('A')\n",
    "print('{:>f} nm = {:>f} A'.format(r_nm, r_a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "4.184\n"
     ]
    }
   ],
   "source": [
    "print(units0.length(10, 'A'))\n",
    "print(units0.energy(1, 'kcal/mol'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.000000 kcal/mol = 4.184000 kJ/mol\n",
      "1.000000 kJ/mol = 0.239006 kcal/mol\n"
     ]
    }
   ],
   "source": [
    "E_kcal = 1\n",
    "E_kj = E_kcal * units0.convert_energy_from('kcal/mol')\n",
    "print('{:>f} kcal/mol = {:>f} kJ/mol'.format(E_kcal, E_kj))\n",
    "E_kj = 1\n",
    "E_kcal = E_kj * units0.convert_energy_to('kcal/mol')\n",
    "print('{:>f} kJ/mol = {:>f} kcal/mol'.format(E_kj, E_kcal))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.00831446261815324\n",
      "138.93545764498225\n"
     ]
    }
   ],
   "source": [
    "print(units0.boltzmann)\n",
    "print(units0.coulomb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0019872042586408316\n",
      "332.0637133006268\n"
     ]
    }
   ],
   "source": [
    "units1 = Units('A', 'kcal/mol')\n",
    "print(units1.boltzmann)\n",
    "print(units1.coulomb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mindspore-1.9",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "768f82fbbece3f0acd424cda825de8aa44edbfaa38d8a3df4ff1437f7ae7861f"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
